package cn.com.fattiger.framework.cms.admin.common.service.impl;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import cn.com.fattiger.framework.cms.admin.common.service.RedisService;

@Service
public class RedisServiceImpl implements RedisService{

    final static Logger logger = Logger.getLogger(RedisServiceImpl.class);

	@Autowired
	private RedisTemplate redisTemplate;
	
	@Resource(name = "redisTemplate")
	private ValueOperations<String, Object> valueOperation;

	@Resource(name = "redisTemplate")
	private ListOperations<String, Object> listOperation;
	
	@Resource(name="redisTemplate")
	private HashOperations<String, String, Object> hashOperation;

	@Override
	public void set(String key, byte[] bs, int seconds) {
		RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
		if (seconds > 0) {
			rc.setEx(key.getBytes(), seconds, bs);
		} else {
			rc.set(key.getBytes(), bs);
		}
		rc.close();
	}
	@Override
	public void set(String key, Object value, int seconds) {
		try {
			if (seconds > 0) {
				valueOperation.set(key, value, seconds, TimeUnit.SECONDS);
			} else {
				valueOperation.set(key, value);
			}
		} catch (Exception e) {

		}finally{
			
		}
	}

	@Override
	public Object get(String key) {
		try {
			return valueOperation.get(key);
		} catch (Exception e) {
			return null;
		}
	}
	@Override
	public void lset(String key, Object value) {
		try {
			this.listOperation.leftPush(key, value);
		} catch (Exception e) {
			logger.error(e);
		}
	}

	@Override
	public byte[] getByte(String key) {
		RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
		byte[] value = rc.get(key.getBytes());
		rc.close();
		return value;
	}


	public void del(String key) {
		this.redisTemplate.delete(key);

	}


	

	@Override
	public void hset(String key, String name, byte[] bs) {
		
		RedisConnection rc = null;
		try {
			 rc = redisTemplate.getConnectionFactory().getConnection();
			rc.hSet(key.getBytes(), name.getBytes(), bs);
			
		} catch (Exception e) {
			logger.info("redis hset 异常：" + e.getStackTrace());
		}finally{
			if (rc!=null) {
				rc.close();
			}
		}

	}
	

	@Override
	public void hset(String key, String name, byte[] bs, Long seconds) {
		if (seconds == null) {
			this.hset(key, name, bs);
		} else {
			try {
				RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
				rc.hSet(key.getBytes(), name.getBytes(), bs);
				rc.expire(key.getBytes(), seconds);
				rc.close();
			} catch (Exception e) {
				logger.info("redis hset（带有过期时间） 异常：" + e.getStackTrace());
			}
		}
	}


	@Override
	public byte[] hget(String key, String propertyName) {
		RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
		byte[] value = rc.hGet(key.getBytes(), propertyName.getBytes());
		rc.close();
		return value;
	}


	@Override
	public long lsize(String key) {
		return this.listOperation.size(key);
	}

	@Override
	public void setnx(String key, String value) {
		this.valueOperation.setIfAbsent(key, value);
	}



	@Override
	public void hdel(String key, String name) {
		try {
			RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
			rc.hDel(key.getBytes(), name.getBytes());
			rc.close();
		} catch (Exception e) {
			logger.info("redis  hdel删除失败：" + e.getStackTrace());
		}
	}

	@Override
	public void setExpire(String key, long seconds) {
		try {
			RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
			rc.expire(key.getBytes(), seconds);
			rc.close();
		} catch (Exception e) {
			logger.info("redis设置过期时间失败：" + e.getStackTrace());
		}

	}

	@Override
	public long incr(String key) {
		return valueOperation.increment(key, 1l);
		
	}

	@Override
	public Object lpop(String key) {
		
		return  listOperation.leftPop(key);
	}

	@Override
	public void hashSet(String key, String hashKey, Object value) {
		hashOperation.put(key, hashKey, value);
	}

	@Override
	public Object hashGet(String key, String hashKey) {
		return hashOperation.get(key, hashKey);
	}

	@Override
	public void hashSet(String key, String name, Object value, Long seconds) {
		try {
			
			hashOperation.put(key, name, value);
			 if (seconds>0) {
				 setExpire(key, seconds);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
		 
	}


}
